package com.immoc.jdbc.common;

import java.sql.*;

public class DbUtils {
    /**
     * 返回数据库链接
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        // 1. 加载注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2. 创建数据库链接
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/immoc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai",
                "root",
                "455843572Lemonib"
        );
        return conn;
    }

    /**
     * 关闭连接
     * @param conn
     */
    public  static void  closeConnection(Connection conn){
        try {
            // 5. 关闭连接，释放资源
            if (conn != null && conn.isClosed() == false) {
                conn.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
